

use "Data/Refinitiv Fig A2.dta", clear

sort IssueDate
gen year = year(IssueDate)
gen month = month(IssueDate)
egen monthyear = group(month year)

sort IssuerName IssueDate
gen maturity = (MaturityDate - IssueDate)/365
gen callable = Callable == "Yes"
gen GO = SourceofPayment == "General Obligation"
gen BQ = BankQualified == "Qualified"
gen insurance = BondInsurance != ""

sort IssuerName year
by IssuerName year: egen sum_AmountIssued = total(AmountIssued)

foreach var in MeanYield maturity callable GO BQ insurance {
replace `var' = `var' * (AmountIssued/sum_AmountIssued)
}

collapse (sum) AmountIssued MeanYield maturity callable GO BQ insurance, by(IssuerName year) 

keep if (BQ == 1 & AmountIssued <= 10) | (BQ == 0 & AmountIssued > 0)

gen maturity_2 = maturity * maturity

sum MeanYield, detail
drop if MeanYield > r(p99) | MeanYield < r(p1)

replace AmountIssued = AmountIssued / 1000000

gen bucket = .
 forvalues x = 6.5(0.5)20 {
 replace bucket = `x' if AmountIssued > `x' - 0.5 & AmountIssued <= `x' 
 }

egen IssuerNum = group(IssuerName)
sort IssuerName year
xtset IssuerNum year

collapse (mean) MeanYield AmountIssued, by(bucket)


reg MeanYield bucket if bucket >= 8 & bucket <= 10
predict q
reg MeanYield bucket if bucket > 10 & bucket < 13
predict q2


twoway scatter MeanYield bucket if bucket >= 8 & bucket < 13, msymbol(smcircle) mfcolor(white) || lfit q bucket if bucket >= 8 & bucket <= 10, lpattern(dash) || lfit q2 bucket if bucket >= 10 & bucket <= 13, lpattern(dash) xtitle("Amount of Debt Issued") ytitle("Yield") legend(label (1 "Observed") label (2 "Counterfactual")) graphregion(color(white)) xline(10) legend(off) yscale(range(2.7(.2)3.9)) ylabel(2.7(.2)3.9) xscale(range(7.5(1)12.5)) xlabel(8(2)12)

graph save "Figure A2", replace